const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

Page({
  data: {
    tabs: ['全部', '未开始', '进行中', '已结束'],
    activeTab: 0,
    activityList: []
  },

  onLoad(options) {
    if (options.status) {
      this.setData({
        activeTab: parseInt(options.status)
      });
    }
    this.loadActivityList();
  },

  onTabChange(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeTab: index
    });
    this.loadActivityList();
  },

  loadActivityList() {
    util.showLoading();
    
    // 模拟数据，实际应从API获取
    setTimeout(() => {
      const mockActivities = [
        {
          id: 1,
          title: '社区健康讲座 - 秋季养生知识',
          image: 'https://dummyimage.com/710x360/4A90E2/fff.png&text=健康讲座',
          time: '2023-11-10 14:00',
          location: '社区活动中心',
          joinCount: 38,
          status: 0,
          statusText: '未开始',
          statusClass: 'upcoming'
        },
        {
          id: 2,
          title: '老年太极拳学习班',
          image: 'https://dummyimage.com/710x360/4CAF50/fff.png&text=太极拳',
          time: '2023-11-05 08:00',
          location: '社区广场',
          joinCount: 52,
          status: 1,
          statusText: '进行中',
          statusClass: 'ongoing'
        },
        {
          id: 3,
          title: '重阳节登山活动',
          image: 'https://dummyimage.com/710x360/FF9800/fff.png&text=登山',
          time: '2023-10-23 07:00',
          location: '西湖风景区',
          joinCount: 45,
          status: 2,
          statusText: '已结束',
          statusClass: 'finished'
        },
        {
          id: 4,
          title: '书法兴趣班体验课',
          image: 'https://dummyimage.com/710x360/9C27B0/fff.png&text=书法',
          time: '2023-10-15 14:00',
          location: '文化活动室',
          joinCount: 28,
          status: 2,
          statusText: '已结束',
          statusClass: 'finished'
        }
      ];

      // 根据activeTab筛选活动
      let filteredActivities = mockActivities;
      if (this.data.activeTab > 0) {
        filteredActivities = mockActivities.filter(activity => activity.status === this.data.activeTab - 1);
      }

      this.setData({
        activityList: filteredActivities
      });
      util.hideLoading();
    }, 500);
  },

  onActivityDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/community/activity/activity?id=${id}`
    });
  },

  onCancel(e) {
    const id = e.currentTarget.dataset.id;
    util.showConfirm('确定要取消报名吗？').then(confirm => {
      if (confirm) {
        // 实际应调用API取消报名
        util.showToast('已取消报名', 'success');
        this.loadActivityList();
      }
    });
  },

  onShare(e) {
    const id = e.currentTarget.dataset.id;
    util.showToast('分享功能');
    // 实际应调用分享API
  },

  onExplore() {
    wx.switchTab({
      url: '/pages/circle/circle'
    });
  },

  onPullDownRefresh() {
    this.loadActivityList();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 500);
  }
})

